﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPCAutomatic.DAL
{
    internal class AChaneelsUp
    {
        private readonly string _connectionString;
        public AChaneelsUp(string connectionString)
        {
            _connectionString = connectionString;
            InitializeAchaneelUpDatabase();
        }
        private void InitializeAchaneelUpDatabase()
        {
            try
            {
                using (var connection = new SQLiteConnection(_connectionString))
                {
                    connection.Open();

                    // 检查AchaneelUp表是否存在
                    string checkTableSql = "SELECT name FROM sqlite_master WHERE type='table' AND name='AchaneelUp'";
                    using (var checkTableCmd = new SQLiteCommand(checkTableSql, connection))
                    {
                        var result = checkTableCmd.ExecuteScalar();
                        if (result == null)
                        {
                            // 表不存在则创建
                            string createTableSql = @"   
CREATE TABLE [AchaneelUp](
    [ID] INTEGER PRIMARY KEY AUTOINCREMENT, 
    [额定电压] TEXT NOT NULL, 
    [额定电流] TEXT NOT NULL)";
                            using (var createTableCmd = new SQLiteCommand(createTableSql, connection))
                            {
                                createTableCmd.ExecuteNonQuery();
                            }

                            // 插入默认行
                            string insertSql = "INSERT INTO [AchaneelUp] ([额定电压], [额定电流]) VALUES ('0', '0')";
                            using (var insertCmd = new SQLiteCommand(insertSql, connection))
                            {
                                insertCmd.ExecuteNonQuery();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("初始化AchaneelUp数据库时出错", ex);
            }
        }
        public (string Voltage, string Current) GetAchaneelUpValues()
        {
            try
            {
                using (var connection = new SQLiteConnection(_connectionString))
                {
                    connection.Open();
                    string selectSql = "SELECT [额定电压], [额定电流] FROM [AchaneelUp] LIMIT 1";
                    using (var selectCmd = new SQLiteCommand(selectSql, connection))
                    {
                        using (var reader = selectCmd.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                return (
                                    reader["额定电压"].ToString(),
                                    reader["额定电流"].ToString()
                                );
                            }
                        }
                    }
                }
                return ("0", "0");
            }
            catch (Exception ex)
            {
                throw new Exception("获取AchaneelUp值时出错", ex);
            }
        }
        public void SaveAchaneelUpValues(string voltage, string current)
        {
            try
            {
                using (var connection = new SQLiteConnection(_connectionString))
                {
                    connection.Open();

                    // 更新记录
                    string updateSql = @"   
UPDATE [AchaneelUp]    
SET [额定电压] = @电压, [额定电流] = @电流    
WHERE ID = (SELECT MIN(ID) FROM [AchaneelUp])";

                    using (var command = new SQLiteCommand(updateSql, connection))
                    {
                        command.Parameters.AddWithValue("@电压", voltage);
                        command.Parameters.AddWithValue("@电流", current);
                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("保存AchaneelUp值时出错", ex);
            }
        }
    }
}
